package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    public void save(DishDTO dishDTO) {
        Dish dish = Dish.builder()
                .id(dishDTO.getId())
                .name(dishDTO.getName())
                .categoryId(dishDTO.getCategoryId())
                .price(dishDTO.getPrice())
                .image(dishDTO.getImage())
                .description(dishDTO.getDescription())
                .status(dishDTO.getStatus())
                .build();
//        向菜品表插入一条数据
        dishMapper.insert(dish);

//        获取dish插入后生成的主键值id(category_id) 用于确保传过来的category_id有值
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(item -> item.setDishId(dishId));
//            向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    public DishVO findById(Long id) {
        Dish dish = dishMapper.selectById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selctByDishId(id);
        return new DishVO(dish.getId(), dish.getName(), dish.getCategoryId(), dish.getPrice(), dish.getImage(), dish.getDescription(), dish.getStatus(),
                dish.getUpdateTime(), dish.getName(), dishFlavors);
    }

    /**
     * 根据分类id查看菜品
     *
     * @param categoryId
     * @return
     */
    public List<Dish> findByCategoryId(long categoryId) {
        List<Dish> dishes = dishMapper.selectByCategoryId(categoryId);
        return dishes;
    }

    /**
     * 商品的分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult findPage(DishPageQueryDTO dishPageQueryDTO) {
        int page = dishPageQueryDTO.getPage();
        int pageSize = dishPageQueryDTO.getPageSize();
        PageHelper.startPage(page, pageSize);
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO, dish);
        List<Dish> dishes = dishMapper.selectDish(dish);
        Page dishes1 = (Page) dishes;
        return new PageResult(dishes1.getTotal(), dishes1);
    }

    /**
     * 菜品起售/停售
     *
     * @param status
     */
    public void startOrStop(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.modify(dish);
    }

    /**
     * 修改商品
     *
     * @param dishDTO
     */
    public void modifyDish(DishDTO dishDTO) {
//        修改商品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.modify(dish);
//        修改商品口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size()!= 0 && flavors!= null) {
            dishFlavorMapper.modify(flavors);
        }
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
//       判断当前菜品能否被删除--是否存在起售中的菜品
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
//                当前菜品处于起售中 不可删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
//        判断当前菜品是否能够删除--是否被套餐关联了
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null && setmealIds.size() > 0) {
//            当前菜品被套餐关联 不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
//        删除菜品表的菜品数据
        dishMapper.deleteById(ids);
//            删除菜品关联的口味数据
        dishFlavorMapper.deleteById(ids);
    }

    /**
     * 条件查询菜品和口味
     * @param categoryId
     * @return
     */
    public List<DishVO> listWithFlavor(Long categoryId) {
        Dish dish = new Dish();
        Dish dish1 = dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> list = dishMapper.list(dish1);


        List<DishVO> dishVOS = new ArrayList<>();

        for (Dish dish2 : list) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish2, dishVO);
            Long id = dish2.getId();
            List<DishFlavor> flavors = dishFlavorMapper.selctByDishId(id);
            dishVO.setFlavors(flavors);
            dishVOS.add(dishVO);
            log.info("dishVOS的值: {}", dishVOS);
        }
        return dishVOS;
    }
}
